//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SimpleChooserOption.h"
using namespace Cephei::QL::Experimental::Exoticoptions;
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/OneAssetOption.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::CSimpleChooserOption (DateTime choosingDate, Double strike, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : COneAssetOption(CSimpleChooserOption::typeid)
{
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phSimpleChooserOption = NULL;
#endif
        QuantLib::Date _choosingDate = (QuantLib::Date)ValueHelper::Convert (choosingDate); //d
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _ppSimpleChooserOption = new boost::shared_ptr<QuantLib::SimpleChooserOption> (new QuantLib::SimpleChooserOption ( _choosingDate,  _strike,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppSimpleChooserOption)->setPricingEngine (_QL_Pricer);
        SetOneAssetOption (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppSimpleChooserOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::CSimpleChooserOption (boost::shared_ptr<QuantLib::SimpleChooserOption>& childNative, Object^ owner) : COneAssetOption(CSimpleChooserOption::typeid)
{
#ifdef HANDLE
	_phSimpleChooserOption = NULL;
#endif
	_ppSimpleChooserOption = &childNative;
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppSimpleChooserOption));
}
Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::CSimpleChooserOption (QuantLib::SimpleChooserOption& childNative, Object^ owner) : COneAssetOption(CSimpleChooserOption::typeid)
{
#ifdef HANDLE
	_phSimpleChooserOption = NULL;
#endif
	_ppSimpleChooserOption = new boost::shared_ptr<QuantLib::SimpleChooserOption> (&childNative);
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppSimpleChooserOption));
    _SimpleChooserOptionOwner = owner;
    _OneAssetOptionOwner = owner;
}

Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::CSimpleChooserOption (CSimpleChooserOption^ copy) : COneAssetOption(CSimpleChooserOption::typeid)
{
#ifdef HANDLE
	_phSimpleChooserOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSimpleChooserOption = new boost::shared_ptr<QuantLib::SimpleChooserOption> (copy->GetShared());
        _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppSimpleChooserOption));
    }
}
Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::CSimpleChooserOption (PLATFORM::Type^ t) : COneAssetOption(CSimpleChooserOption::typeid)
{
#ifdef HANDLE
	_phSimpleChooserOption = NULL;
#endif
	if (!t->IsSubclassOf(CSimpleChooserOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::CSimpleChooserOption (QuantLib::Handle<QuantLib::SimpleChooserOption>& childNative, Object^ owner)  : COneAssetOption(CSimpleChooserOption::typeid)
{
	_phSimpleChooserOption = &childNative;
	_ppSimpleChooserOption = &static_cast<boost::shared_ptr<QuantLib::SimpleChooserOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppSimpleChooserOption));
    _SimpleChooserOptionOwner = owner;
}
Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::CSimpleChooserOption (QuantLib::Handle<QuantLib::SimpleChooserOption> childNative)  : COneAssetOption(CSimpleChooserOption::typeid)
{
	_phSimpleChooserOption = &childNative;
	_ppSimpleChooserOption = &static_cast<boost::shared_ptr<QuantLib::SimpleChooserOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppSimpleChooserOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::CSimpleChooserOption (QuantLib::SimpleChooserOption childNative)  : COneAssetOption(CSimpleChooserOption::typeid)
{
#ifdef HANDLE
	_phSimpleChooserOption = NULL;
#endif
	_ppSimpleChooserOption = new boost::shared_ptr<QuantLib::SimpleChooserOption> (new QuantLib::SimpleChooserOption (childNative));
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppSimpleChooserOption));
}
#endif

Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::~CSimpleChooserOption ()
{
    if (_ppSimpleChooserOption != NULL)
    {
	    delete _ppSimpleChooserOption;
        _ppSimpleChooserOption = NULL;
    }
}
Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::!CSimpleChooserOption ()
{
    if (_ppSimpleChooserOption != NULL)
    {
	    delete _ppSimpleChooserOption;
    }
}
QuantLib::SimpleChooserOption& Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::GetReference ()
{
    if (_ppSimpleChooserOption == NULL) throw REFNEW NativeNullException ();
	return **_ppSimpleChooserOption;
}
boost::shared_ptr<QuantLib::SimpleChooserOption>& Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::GetShared ()
{
    if (_ppSimpleChooserOption == NULL) throw REFNEW NativeNullException ();
	return *_ppSimpleChooserOption;
}
QuantLib::SimpleChooserOption* Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::GetPointer ()
{
    if (_ppSimpleChooserOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppSimpleChooserOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SimpleChooserOption>& Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::GetHandle ()
{
	if (_phSimpleChooserOption == NULL)
	{
		_phSimpleChooserOption = new Handle<QuantLib::SimpleChooserOption> (*_ppSimpleChooserOption);
	}
	return *_phSimpleChooserOption;
}
#endif
bool Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption::HasNative () 
{
	return (_ppSimpleChooserOption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Exoticoptions::ISimpleChooserOption^ Cephei::QL::Experimental::Exoticoptions::CSimpleChooserOption_Factory::Create (DateTime choosingDate, Double strike, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CSimpleChooserOption ( choosingDate,  strike,  exercise,  QL_Pricer);
}
